import win32com.client as wc


# 强制关闭Excel进程
def close_excel_by_force(excel):
    import win32process
    import win32api
    import win32con
    # Get the window's process id's
    hwnd = excel.Hwnd
    t, p = win32process.GetWindowThreadProcessId(hwnd)
    # Ask window nicely to close
    try:
        handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, p)
        if handle:
            win32api.TerminateProcess(handle, 0)
            win32api.CloseHandle(handle)
    except:
        pass


ExcelApp = wc.Dispatch('Excel.Application')
...
...
close_excel_by_force(ExcelApp)
del ExcelApp
